package com.work.xxzl.module.main;


import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebHistoryItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.work.xxzl.R;
import com.work.xxzl.base.BaseActivity;
import com.work.xxzl.utils.LogUtils;

/**
 * author：WangZhan
 * data: 2017/2/17 10:40
 * version: 1.0.0
 * explain:加载网页
 */

public class WebViewActivity extends BaseActivity {
    private ProgressBar progressBar;
    private WebView webView;
    private String weburl;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        mTitleBar.titleMiddle.setText("标题");
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        webView = (WebView) findViewById(R.id.webView);
    }

    @Override
    protected void initData() {
        initwebview();
//        weburl = getIntent().getStringExtra("url");
        weburl = "https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_6698100589169763297%22%7D&n_type=0&p_from=1";
        LogUtils.d("weburl=" + weburl);
        //加载网页
        webView.loadUrl(weburl);
    }

    private void initwebview() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.loadUrl(weburl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.GONE);
                } else {
                    // 加载中
                    progressBar.setSecondaryProgress(newProgress);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                //获取网页标题
                getWebTitle();
            }
        });
    }

    //获取网页标题
    private void getWebTitle() {
        WebBackForwardList forwardList = webView.copyBackForwardList();
        WebHistoryItem item = forwardList.getCurrentItem();
        if (item != null) {
            mTitleBar.titleMiddle.setText(item.getTitle());
        }
    }

    private void onWebViewGoBack() {
        webView.goBack();
        getWebTitle();
    }

    //改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {//返回上一页面
                webView.goBack();
                onWebViewGoBack();
                return true;
            } else {//退出程序
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
